set more off

use dataset, clear

* program for substantive effects
capture program drop myest
program define myest
	
	quietly{
	
		args model x nreps
		gen B`model' = .
		gen SE`model' = .
		gen EB`model' = .
		gen ESE`model' = .
		
		// coef estimate
		local myX = subinstr("`e(cmdline)'", "regress ", "", 1)
		reg `myX'

		replace B`model' = _b[`x'] if _n == `model'
		replace SE`model' = _se[`x'] if _n == `model'

		
		// coef SE
		
		// subs effect point estimate
		preserve
			replace `x' = 0
			predict yhat, xb
			su yhat
		restore
		local y0 = r(mean)
		preserve
			replace `x' = 1
			predict yhat, xb
			su yhat
		restore
		local y1 = r(mean)
		replace EB`model' = exp(`y1')-exp(`y0') if _n == `model' 
		// subs effect standard error
		capture erase tmp0.dta
		gen B = .
		save tmp0, replace
		nois _dots 0, title(Loop running) reps(`nreps')
		forvalues i = 1(1)`nreps' {
			use tmp0, clear
			bsample
			local myX = subinstr("`e(cmdline)'", "regress ", "", 1)
			preserve
				qui: reg `myX'
			restore
			preserve
				replace `x' = 0
				predict yhat, xb
				su yhat
			restore
			local y0 = r(mean)
			preserve
				replace `x' = 1
				predict yhat, xb
				su yhat
			restore
			local y1 = r(mean)
			use tmp0, clear
			replace B = exp(`y1')-exp(`y0') if _n == `i'
			save tmp0, replace
			nois _dots `i' 0
		}
		su B
		use tmp0, clear
		erase tmp0.dta
		drop B
		replace ESE`model' = r(sd) if _n == `model'

	
	}
	
end

*** ***

replace pctblack = exp(pctblack) - 1
gen underrepresentation = pctblack - pctblackgov
replace underrep = 0 if underrep < 0
replace underrep = . if mi(pctblackgov)
replace underrep = log(under + 1)


local X1 totalrevownsourcespcp pcttotaltax pcttotalig
local X2 lpopulation popdensity incomepcp pctba pct65plus H pcthisp herf pctforeign
local X3 crimepcp totalpolicepcp dem ngovspcp netmigration 

local X `X1' `X2' `X3' `X4'

foreach x in `X' pctblack underrep {
	su `x'
	replace `x' = (`x' - r(min)) / (r(max) - r(min))
	drop if mi(`x')
}

*** ***

local nrep 500

reg lfinesandforfeitspcp underrep, robust
myest 1 underrep `nrep'

reg lfinesandforfeitspcp underrep `X', robust
myest 2 underrep `nrep'

collapse B* SE* EB* ESE*

collapse B* SE* EB* ESE*

gen index = _n
reshape i index
reshape j model
reshape xi
reshape xij B SE EB ESE
reshape long
drop index model

gen myp = 2*(1-normal(abs(B/SE)))
gen mystars = ""
replace mystars = mystars + "*"  if myp < 0.05
replace mystars = mystars + "*"  if myp < 0.01
replace mystars = mystars + "*"  if myp < 0.001

foreach x in B SE EB ESE {
	gen `x'x = string(`x', "%20.02fc")
	drop `x'
	rename `x'x `x'
}
foreach x in SE ESE {
	replace `x' = "(" + `x' + ")"
}
replace B = B + mystars
drop mystars myp

sxpose, clear force

expand 3

forvalues i = 1(1)2 {
	replace _var`i' = _var`i'[_n-1] if _n == 5 
	replace _var`i' = _var`i'[_n-1] if _n == 4
	replace _var`i' = " " if _n == 3 | _n >= 6
}

gen mylab = " "
order mylab
replace mylab = "Percent black population" if _n == 1
replace mylab = "Effect size (\\$)" if _n == 4
replace mylab = "Controls" if _n == 6

replace _var2 = "\checkmark" if inlist(mylab, "Controls")

keep if _n < 7

l

#delimit;

listtex 
	using "tableA7.tex"
	,
	replace
	type rstyle(tabular)
	head(
		"\begin{tabular*}{\hsize}{@{\hskip\tabcolsep\extracolsep\fill}l*{3}{c}}"
		"\toprule&(1)&(2)""\\
		\midrule"
	)
	foot("\bottomrule\end{tabular*}")
	;
	
#delimit cr
